<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/

/** @see Zead_Manifest_Writer_Xml **/
require_once 'Zead/Manifest/Writer/Xml.php';

/**
 * @category   Zead
 * @package    Zead_Manifest
 * @subpackage Zead_Manifest_Adapter_Xml
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License 
 */
class Zead_Manifest_Writer_Xml_Scorm extends Zead_Manifest_Writer_Xml
{
	public function write(Zead_Manifest $manifest)
	{
		$simplexml = new SimpleXmlElement('<manifest/>');
		
		// Creating metadata
		$metadata = $simplexml->addChild('metadata');
		$metadata->addChild('schema', 'ADL SCORM');
		$metadata->addChild('schemaVersion', '1.2');
		
		// Creating LOM
		$lom = $metadata->addChild('lom');
		$general = $lom->addChild('general');
		$general->addChild('title', 'Default Organization');

		// Parse contents
		$organizations = $simplexml->addChild('organizations');	
		$organizations->addAttribute('default', 'DEFAULT_ITEM_ORG');
		
		// Create the organization default
		$organization = $organizations->addChild('organization');
		$organization->addAttribute('identifier', 'DEFAULT_ITEM_ORG');
		$organization->addAttribute('structure',  'hierarquical');
		$organization->addChild('title', 'Default Organization');
		
		// Creating resources
		$resources = $simplexml->addChild('resources');	
			
		foreach ($manifest->getContents()->getChildren() as $child) {
			// Creating the identifier
			$identifier = 'SCO_'. strtoupper($child->name);
			$identifierref = $identifier . '_IDREF';
		
			// Creating item (SCO)
			$item = $organization->addChild('item');
			$item->addAttribute('identifier', $identifier);
			$item->addAttribute('identifierref', $identifierref);
			$item->addChild('title', $child->title);
			
			// Creating resource
			$resource = $resources->addChild('resource');
			$resource->addAttribute('identifier', $identifierref);			
			$resource->addAttribute('type', 'webcontent');
			$resource->addAttribute('href', 'index.html');
			$resource->addAttribute('adlcp:scormtype', 'sco');
			
			// Add files to resource
			for ($i = 1; $i < 10; $i++) {
				$file = $resource->addChild('file');
				$file->addAttribute('href', 'file_'. $i . '.html');
			}
		}		
	
		if (isset($this->_options['file'])) {

			$doc = new DOMDocument();
			$doc->appendChild($doc->importNode(dom_import_simplexml($simplexml),true));
			$doc->formatOutput = true;
			$doc->save($this->_options['file']);
		}		
	}
}
